package vip.fuxk.blog.config;

import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.PathRule;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vip.fuxk.blog.common.R;

import java.io.PrintWriter;
import java.io.StringWriter;

@Component
public class ErrorInterceptorImpl implements RouterInterceptor {


    Logger logger = LoggerFactory.getLogger(ErrorInterceptorImpl.class);


    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {

        try{
            chain.doIntercept(ctx, mainHandler);
        }catch (Throwable e){

            e.printStackTrace();
            logger.error(e.getMessage(),e);
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            ctx.render(R.error(errors.toString()));
        }


    }
}